import {
  Column,
  CreateDateColumn, DeleteDateColumn,
  Entity,
  JoinTable,
  ManyToMany,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn
} from "typeorm";
import { Role } from '@/modules/role/entities/role.entity';
import { Article } from '@/modules/article/entities/article.entity';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({
    length: 50,
    comment: '用户名',
  })
  username: string;

  @Column({
    comment: '密码',
    select: false,
  })
  password: string;

  @Column({
    length: 50,
    comment: '昵称',
  })
  nickName: string;

  @Column({
    comment: '邮箱',
    length: 50,
  })
  email: string;

  @Column({
    comment: '头像',
    length: 100,
    nullable: true,
  })
  headPic: string;

  // 性别
  @Column({ nullable: true })
  gender: number;

  // 出生日期
  @Column({ nullable: true })
  birthdayDate: Date;

  @Column({
    comment: '手机号',
    length: 20,
    nullable: true,
  })
  phoneNumber: string;

  @Column({
    default: true,
  })
  status: boolean;

  // 上次选中的角色，用于再次登录时默认选中，如果为空选中第一个
  @Column({ nullable: true })
  lastRoleCode: string;

  // 上一次登录时间
  @Column({ nullable: true })
  lastLoginTime: Date;

  // 上一次登录IP
  @Column({ nullable: true })
  lastLoginIp: string;

  @CreateDateColumn()
  createTime: Date;

  @UpdateDateColumn()
  updateTime: Date;

  @DeleteDateColumn({ select: false })
  deletedAt: Date;

  // 关联Role
  @ManyToMany(() => Role, (role) => role.users, {
    createForeignKeyConstraints: false,
  })
  @JoinTable()
  roles: Role[];

  @OneToMany(() => Article, (article) => article.author)
  articles: Article[];
}
